import 'package:html_unescape/html_unescape.dart';

class Quiz {
  int responseCode = 0;
  List<Results> results = [];

  Quiz({required this.responseCode, required this.results});

  Quiz.fromJson(Map<String, dynamic> json) {
    responseCode = json['response_code'];
    results = [];
    if (json['results'] != null) {
      json['results'].forEach((v) {
        results.add(Results.fromJson(v));
      });
    }
  }

  Map<String, dynamic> toJson() {
    final Map<String, dynamic> data = Map<String, dynamic>();
    data['response_code'] = responseCode;
    data['results'] = results.map((v) => v.toJson()).toList();
    return data;
  }
}

class Results {
  String category = "";
  String type = "";
  String difficulty = "";
  String question = "";
  String correctAnswer = "";
  List<String> allAnswers = [];

  Results({
    required this.category,
    required this.type,
    required this.difficulty,
    required this.question,
    required this.correctAnswer,
  });

  //从Json数据中获取题目信息
  Results.fromJson(Map<String, dynamic> json) {
    var unescape = HtmlUnescape();
    category = json['category'];
    type = json['type'];
    difficulty = json['difficulty'];
    question = unescape.convert(json['question']);
    correctAnswer = json['correct_answer'];
    allAnswers = json['incorrect_answers'].cast<String>();
    allAnswers.add(correctAnswer);
    allAnswers.shuffle();
  }

  Map<String, dynamic> toJson() {
    final Map<String, dynamic> data = Map<String, dynamic>();
    data['category'] = category;
    data['type'] = type;
    data['difficulty'] = difficulty;
    data['question'] = question;
    data['correct_answer'] = correctAnswer;
    data['incorrect_answers'] = allAnswers;
    return data;
  }
}
